﻿using System;
using System.Linq;
using System.Net;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Ayatta.Contract.Domain;
using Web.Manage.Controllers;
using Web.Manage.Areas.Sys.Models;

namespace Web.Manage.Areas.Sys.Controllers
{
    public class EmailController : AdminBaseController
    {

        public EmailController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(string name)
        {
            var model = new EmailControllerModel.Index();
            var accounts = BaseService.GetAllEmailAccount();
            if (string.IsNullOrEmpty(name))
            {
                model.Accounts = accounts;
            }
            else
            {
                ViewBag.Name = name;
                model.Accounts = accounts.Where(o => o.DisplayName.Contains(name)).ToList();
            }
            return PartialView(model);
        }

        public ActionResult Account(int? param)
        {
            var model = new EmailAccount();
            if (param.HasValue && param.Value > 0)
            {
                model = BaseService.GetEmailAccount(param.Value);
            }
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult Account(int? param, EmailAccount model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;
            if (param.HasValue && param.Value > 0)
            {
                var old = BaseService.GetEmailAccount(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = BaseService.UpdateEmailAccount(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/sys/email") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = BaseService.CreateEmailAccount(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/sys/email");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        [HttpPost]
        public ActionResult DeleteAccount(int? param)
        {
            if (param.HasValue && param.Value > 0)
            {
                var result = BaseService.DeleteEmailAccount(param.Value);
                return result ? Json(HttpStatusCode.OK, "删除成功", false, "/sys/email") : Json(HttpStatusCode.BadRequest, "删除失败");

            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        #region Template
        public ActionResult Templates(string name)
        {
            var model = new EmailControllerModel.Templates();
            var templates = BaseService.GetAllEmailTemplate();
            if (string.IsNullOrEmpty(name))
            {
                model.EmailTemplates = templates;
            }
            else
            {
                model.Name = name;
                model.EmailTemplates = templates.Where(o => o.Name.Contains(name)).ToList();
            }
            return PartialView(model);
        }

        public ActionResult Template(int? param)
        {
            var model = new EmailTemplate();
            if (param.HasValue && param.Value > 0)
            {
                model = BaseService.GetEmailTemplate(param.Value);
            }
            return PartialView(model);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Template(int? param, EmailTemplate model)
        {
            var dateTime = DateTime.Now;
            var userName = User.Identity.Name;
            if (param.HasValue && param.Value > 0)
            {
                var old = BaseService.GetEmailTemplate(param.Value);
                if (TryUpdateModel(old))
                {
                    var result = BaseService.UpdateEmailTemplate(old);
                    return result ? Json(HttpStatusCode.OK, "更新成功", true, "/sys/email/templates") : Json(HttpStatusCode.BadRequest, "更新失败");
                }
            }
            else
            {
                model.CreatedBy = userName;
                model.CreatedOn = dateTime;
                var result = BaseService.CreateEmailTemplate(model);
                if (result)
                {
                    return Json(HttpStatusCode.OK, "添加成功", true, "/sys/email/templates");
                }
            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }

        [HttpPost]
        public ActionResult DeleteTemplate(int? param)
        {
            if (param.HasValue && param.Value > 0)
            {
                var result = BaseService.DeleteEmailTemplate(param.Value);
                return result ? Json(HttpStatusCode.OK, "删除成功", false, "/sys/email/templates") : Json(HttpStatusCode.BadRequest, "删除失败");

            }
            return Json(HttpStatusCode.BadRequest, "失败");
        }
        #endregion
    }
}
